//
//  ImageOfCheckResultViewController.m
//  yilutongxing
//
//  Created by T&J on 2017/12/6.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "ImageOfCheckResultViewController.h"
#import <IQKeyboardManager.h>
#import <Masonry.h>
#import "DzyImgPicker.h"
#import "RPCheckNote.h"

@interface ImageOfCheckResultViewController ()<UIGestureRecognizerDelegate, UITextViewDelegate, DzyImgDelegate>
@property (strong, nonatomic) IBOutlet UIView *bigbg;
@property (strong, nonatomic) IBOutlet UIView *image_bg;
@property (strong, nonatomic) IBOutlet UITextView *desc_input;
@property (strong, nonatomic) IBOutlet UILabel *label_placehoder;
@property (strong, nonatomic) DzyImgPicker *imgPicker;

@property (strong, nonatomic) NSArray *image_arr;
@property (strong, nonatomic) NSString *desc;

@end

@implementation ImageOfCheckResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //self.title = @"图片报告";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save)];
    UIImage *leftImage=[[UIImage imageNamed:@"返回"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *leftButtonItem=[[UIBarButtonItem alloc]initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem=leftButtonItem;
    
    [self initUI];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

#pragma mark - UI事件
-(void)onTapThisView{
    [self.desc_input resignFirstResponder];
}

-(void)save{
    self.desc = self.desc_input.text;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(backImage:andDesc:forIndex:)]) {
        [self.delegate backImage:self.image_arr andDesc:self.desc forIndex:self.indexPath];
    }
    
    [self back];
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITextViewDelegate
-(void)textViewDidChange:(UITextView*)textView {
    if([textView.text length] == 0){
        self.label_placehoder.text = @"请在此输入对影像报告的文字说明";
    }else{
        self.label_placehoder.text = @" ";
    }
}
//设置超出最大字数（1000字）即不可输入 也是textview的代理方法
-(BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {
    if (range.location >= 1000) {
        return NO;
    }else {
        return YES;
    }
}

#pragma mark - UI构建
-(void)initUI{
    [self buildImagePicker];
    [self buildDoneButton];
}

//图片选择器
-(void)buildImagePicker{
    CGSize image_bg_size = self.image_bg.frame.size;
    self.imgPicker = [[DzyImgPicker alloc] initWithFrame:CGRectMake(0, 0, image_bg_size.width, 115)
                                                       andParentV:self
                                                        andMaxNum:3];
    self.imgPicker.dzyImgDelegate = self;
    self.imgPicker.collectionView.scrollEnabled = NO;
    [self.image_bg addSubview:self.imgPicker];
}

//键盘收回按钮
-(void)buildDoneButton{
    self.desc_input.delegate = self;
    UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onTapThisView)];
    bar.items = [NSArray arrayWithObject:btn];
    self.desc_input.inputAccessoryView = bar;
}

#pragma mark - 数据初始化
-(void)initData{
    if (_bean) {
        if (_bean.images && _bean.images.count>0) {
            [self.imgPicker setImages:_bean.images];
        }
        if (_bean.desc && [_bean.desc isNotEmpty]) {
            self.label_placehoder.text = @" ";
            self.desc_input.text = _bean.desc;
        }
    }
    else {
        _bean = [[RPImageChecknote alloc]init];
    }
}

#pragma mark - DzyImgDelegate
- (void)getImages:(NSArray *)imgData{
    self.image_arr = imgData;
}

@end
